package com.lingnan.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lingnan.eduservice.entity.po.EduSubjectPO;
import com.lingnan.eduservice.entity.vo.EduSubjectVO;
import com.lingnan.eduservice.entity.vo.EduOneSubjectVO;
import com.lingnan.eduservice.entity.vo.EduTwoSubjectVO;
import com.lingnan.eduservice.listener.EduSubjectExcelListener;
import com.lingnan.eduservice.mapper.EduSubjectMapper;
import com.lingnan.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lingnan.servicebase.excetioncontrooller.LingNanException;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author zzd
 * @since 2021-08-16
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubjectPO> implements EduSubjectService {
    /**
     * Excel批量导入课程
     *
     * @param file
     */
    @Override
    public void addSubjectByExcel(MultipartFile file, EduSubjectService eduSubjectService) {
        try {
            // 获取文件输入流
            InputStream inputStream = file.getInputStream();

            // 使用EasyExcel读取
            EasyExcel.read(inputStream, EduSubjectVO.class, new EduSubjectExcelListener(eduSubjectService)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
            throw new LingNanException(20002, "添加课程分类失败");

        }
    }

    /**
     * 查询所有课程信息(树形展示)
     *
     * @return
     */
    @Override
    public List<EduOneSubjectVO> findAllSubjectByTree() {

        // 1.查询所有一级课程信息
        QueryWrapper<EduSubjectPO> oneSubjectWrapper = new QueryWrapper<>();
        // 封装查询条件
        oneSubjectWrapper.eq("parent_id", "0");
        // 执行查询
        List<EduSubjectPO> oneSubjectList = baseMapper.selectList(oneSubjectWrapper);


        // 2.查询所有二级课程信息
        QueryWrapper<EduSubjectPO> twoSubjectWrapper = new QueryWrapper<>();
        // 封装查询条件
        twoSubjectWrapper.ne("parent_id", "0");
        // 执行查询
        List<EduSubjectPO> twoSubjectList = baseMapper.selectList(twoSubjectWrapper);

        // 定义最终返回列表信息
        List<EduOneSubjectVO> finalSubjectList = new ArrayList<>(100);

        // 创建一级课程分类对象(中转站)
        EduOneSubjectVO eduOneSubjectVO = null;
        // 创建二级课程分类对象(中转站)
        EduTwoSubjectVO eduTwoSubjectVO = null;
        // 创建二级课程分类集合(中转站)
        List<EduTwoSubjectVO> eduTwoSubjectVOList = null;

        // 3.封装一级课程信息
        for (EduSubjectPO oneEduSubjectPO : oneSubjectList) {

            eduOneSubjectVO = new EduOneSubjectVO();

            eduTwoSubjectVOList = new ArrayList<>(100);

            // 拷贝
            BeanUtils.copyProperties(oneEduSubjectPO, eduOneSubjectVO);

            // 4.封装二级课程信息
            for (EduSubjectPO twoEduSubjectPO : twoSubjectList) {
                // 判断父ID是否相同
                if(eduOneSubjectVO.getId().equals(twoEduSubjectPO.getParentId())){
                    // 初始化
                    eduTwoSubjectVO = new EduTwoSubjectVO();
                    // 拷贝
                    BeanUtils.copyProperties(twoEduSubjectPO, eduTwoSubjectVO);
                    // 添加到临时二级课程分类集合
                    eduTwoSubjectVOList.add(eduTwoSubjectVO);
                }
            }
            // 设置二级课程分类集合
            eduOneSubjectVO.setEduTwoSubjectVOList(eduTwoSubjectVOList);

            // 添加到最终列表
            finalSubjectList.add(eduOneSubjectVO);

        }
        return finalSubjectList;
    }
}
